package com.utc.cards.common.view.listener;

import android.content.Context;
import android.graphics.Color;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.widget.Toast;

import com.utc.cards.common.view.CardsContainer;
import com.utc.cards.model.PlayerModel;
import com.utc.cards.model.card.Card;

public abstract class SendDragListener implements OnDragListener
{

    private CardsContainer _container;
    private Context _context;

    private View _selectedView;

    public static boolean MESSAGE_DEJA_AFFICHE = false;

    public SendDragListener(Context context, CardsContainer container)
    {
        _context = context;
        _container = container;
    }

    @Override
    public boolean onDrag(View view, DragEvent dragEvent)
    {

        int action = dragEvent.getAction();

        switch (action)
        {
        case DragEvent.ACTION_DRAG_STARTED:
            // view.setBackgroundColor(Color.YELLOW);
            _selectedView = (View) dragEvent.getLocalState();
            break;

        case DragEvent.ACTION_DRAG_ENTERED:
            if (PlayerModel.Instance().isMyTurnToPlay())
                view.setBackgroundColor(Color.BLACK);
            else
            {
                if (MESSAGE_DEJA_AFFICHE == false)
                {
                    String text = "Ce n'est pas à votre tour de jouer";
                    Toast.makeText(_context, text, Toast.LENGTH_SHORT).show();
                    MESSAGE_DEJA_AFFICHE = true;
                }
            }
            break;

        case DragEvent.ACTION_DRAG_EXITED:
            view.setBackgroundColor(Color.GRAY);
            break;

        case DragEvent.ACTION_DRAG_LOCATION:

            break;

        case DragEvent.ACTION_DROP:
            if (PlayerModel.Instance().isMyTurnToPlay() && _selectedView != null)
            {
                Card card = (Card) _selectedView.getTag();
                String cardName = card.getName();

                System.out.println("Sending card: " + cardName);

                sendCard(card);

                // CardView c = _container.getCardByName(cardName);
                // _container.removeCard(c);
            }

            break;

        case DragEvent.ACTION_DRAG_ENDED:
            view.setBackgroundColor(Color.GRAY);
            MESSAGE_DEJA_AFFICHE = false;
            break;

        default:
            break;
        }

        return true;
    }

    public abstract void sendCard(Card card);

}
